<template>
  <div class="app-container">
    <basic-container>
      <avue-crud ref="crud"
                 v-model="jkdnForm"
                 :page="jkdnPage"
                 :data="tableData"
                 :option="tableOption"
                 :table-loading="jkdnLoading"
                 @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @row-save="rowSave"
                 @row-del="rowDel">
        <template slot-scope="scope"
                  slot="menuLeft">
          <el-button icon="el-icon-plus"
                     size="small"
                     type="primary"
                     @click="handleAdd">新增</el-button>
          <el-button icon="el-icon-back"
                     size="small"
                     type="primary"
                     @click="handleBack">返回</el-button>
          <el-button @click="synResult"
                     icon="el-icon-upload"
                     size="small"
                     type="primary">同步结果</el-button>
        </template>
        <template slot-scope="scope"
                  slot="menu">
          <el-button @click="synResult(scope.row)"
                     icon="el-icon-upload"
                     size="small"
                     type="primary">体检同步</el-button>
          <el-button @click="rowEdit(scope.row)"
                     icon="el-icon-edit"
                     size="small"
                     type="primary">编辑</el-button>
          <el-button @click="rowDel(scope.row)"
                     icon="el-icon-delete"
                     size="small"
                     type="danger">删除</el-button>
          <el-button @click="rowReport(scope.row)"
                     icon="el-icon-edit"
                     size="small"
                     type="primary">生成报告</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { timeFormatStr } from '@/filters/'
import { setStore, getStore } from '@/util/store'
import tableOption from '@/const/cust/health/list'
import { fetchList, synResult } from '@/api/cust/health/list'
import { delObj, addObj } from '@/api/cust/health/jkdn'
export default {
  name: 'crud',
  data () {
    return {
      grjbxxid: null,
      cust: {},
      url: [],
      searchFrom: {},
      jkdnForm: {},
      jkdnLoading: false,
      jkdnPage: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      tableOption: tableOption,
      tableData: []
    }
  },
  created () {
    this.cust = getStore({ name: 'cust' })
    this.grjbxxid = this.$route.query.grjbxxid
    this.getList()
  },
  mounted: function () { },
  computed: {},
  methods: {
    synResult (row) {
      var param = {}
      if (!this.validatenull(row.grjbxxid)) {
        Object.assign(param, row)
        param.type = 'none'
      } else {
        param.type = 'all'
      }
      this.jkdnLoading = true
      synResult(param).then(res => {
        this.$message({
          showClose: true,
          message: '同步成功',
          type: 'success'
        })
      })
      this.jkdnLoading = false
    },
    //搜索
    searchChange (params) {
      this.searchform = params
      this.getList(params)
    },
    //刷新
    refreshChange () {
      this.getList(this.searchform)
    },
    //返回基本信息
    handleBack () {
      this.$router.push(`/cust/base`)
    },
    //新增
    handleAdd: function () {
      this.$refs.crud.rowAdd()
      setTimeout(() => {
        this.jkdnForm = getStore({ name: 'org' })
        var date = new Date()
        this.jkdnForm.jdrq =
          date.getFullYear() +
          '-' +
          (date.getMonth() + 1) +
          '-' +
          date.getDate()
        this.jkdnForm.dnbh = timeFormatStr(new Date(), 'yyyyMMddhhmmss')
      }, 300)
    },
    rowSave (row, done) {
      row.name = this.cust.name
      row.sfzh = this.cust.sfzh
      row.lxdh = this.cust.lxrdh
      row.grjbxxid = this.grjbxxid
      row.cwhmc = row.$cwhmcid
      addObj(row).then(res => {
        this.$message({
          showClose: true,
          message: '添加成功',
          type: 'success'
        })
        setStore({ name: 'jkdn', content: res.data })

        this.$router.push({
          path: `/cust/jkdn/${res.data.id}`,
          query: {
            grjbxxid: res.data.grjbxxid,
            id: res.data.id,
            sfzh: res.data.sfzhm,
            where: true
          }
        })
        done()
      })
    },
    //删除
    rowDel (row) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row.id).then(() => {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.getList(this.searchform)
          })
        })
        .catch(function () { })
    },
    rowEdit (row) {
      setStore({ name: 'jkdn', content: row })
      var where = false
      if (!this.validatenull(this.grjbxxid)) {
        where = true
      }
      this.$router.push({
        path: `/cust/jkdn/${row.id}`,
        query: {
          grjbxxid: row.grjbxxid,
          id: row.id,
          sfzh: row.sfzhm,
          where: where
        }
      })
    },
    //跳转报告页面
    rowReport (row) {
      setStore({ name: 'jkdn', content: row })
      this.$router.push({
        path: `/cust/report`,
        query: {
          grjbxxid: row.grjbxxid,
          id: row.id,
          sfzh: row.sfzhm,
          tmcode: row.tmcode
        }
      })
    },
    //获取列表
    getList (params) {
      this.jkdnLoading = true
      fetchList(
        Object.assign(
          {
            start: (this.jkdnPage.currentPage - 1) * this.jkdnPage.pageSize,
            length: this.jkdnPage.pageSize
          },
          params,
          { grjbxxid: this.$route.query.grjbxxid }
        )
      ).then(res => {
        this.jkdnLoading = false
        this.jkdnPage.total = res.data.recordsTotal
        this.tableData = res.data.data
      })
    }
  }
}
</script>